package com.endpoint.book.controller;


import com.endpoint.book.dto.BookContentAboutRespDto;
import com.endpoint.book.entity.BookContent;
import com.endpoint.book.service.BookContentService;
import com.endpoint.book.to.BookContentTo;
import com.endpoint.common.utils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 小说内容表 前端控制器
 * </p>
 *
 * @author Jack_Li
 * @since 2024-07-03
 */
@Slf4j
@RestController
@RequestMapping("/book/bookContent")
public class BookContentController {

    @Autowired
    private BookContentService bookContentService;

    @GetMapping("/content/{chapterId}")
    public ResultBean<BookContentAboutRespDto> getBookContentAbout(@PathVariable("chapterId") Long chapterId,
                                                                   @RequestHeader (name = "Authorization",required = false) String token) {
        return bookContentService.getBookContentAbout(chapterId, token);
    }

    @GetMapping("/nextChapterId/{chapterId}")
    public ResultBean<BookContentAboutRespDto> getBookNextChapterContent(@PathVariable("chapterId") Long chapterId) {
        return bookContentService.getBookNextChapterContent(chapterId);
    }

    @GetMapping("/preChapterId/{chapterId}")
    public ResultBean<BookContentAboutRespDto> getBookPreChapterContent(@PathVariable("chapterId") Long chapterId) {
        return bookContentService.getBookPreChapterContent(chapterId);
    }

    //保存章节内容
    @PostMapping("/saveContent")
    public ResultBean<Void> saveContent(@RequestBody BookContentTo bookContent){
        log.warn("保存章节内容：{}", bookContent);
        BookContent data = new BookContent();
        BeanUtils.copyProperties(bookContent,data);
        bookContentService.save(data);
        return ResultBean.ok();
    }

}

